package com.itheima.consumer.listener;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Objects;

/**
 * @version 1.0
 * @Author moresuo
 * @Date 2024/9/14 12:33
 * @注释
 */
@Component
@Slf4j
public class SpringRabbitListener {

    @RabbitListener(queues = "simple.queue")
    public void listenerSimpleQueueMessage(String msg) {
        System.out.println("spring 消费者接收到消息：[" + msg + "]");
    }

    @RabbitListener(queues = "work.queue")
    public void listenWorkQueue1(String msg) throws InterruptedException {
        log.error("消费者1接收到消息：[" + msg + "]");
        Thread.sleep(20);
    }

    @RabbitListener(queues = "work.queue")
    public void listenWorkQueue2(String msg) throws InterruptedException {
        log.error("消费者2接收到消息：[" + msg + "]");
        Thread.sleep(200);
    }

    @RabbitListener(queues = "fanout.queue1")
    public void testFanoutQueue1(String msg) {
        System.out.println("消费者1接收到Fanout的消息：[" + msg + "]");
    }

    @RabbitListener(queues = "fanout.queue2")
    public void testFanoutQueue2(String msg) {
        System.out.println("消费者2接收到Fanout的消息：[" + msg + "]");
    }

    @RabbitListener(queues = "direct.queue1")
    public void testDirectQueue1(String msg) {
        System.out.println("消费者1接收到Direct的消息：[" + msg + "]");
    }

    @RabbitListener(queues = "direct.queue2")
    public void testDirectQueue2(String msg) {
        System.out.println("消费者2接收到Direct的消息：[" + msg + "]");
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue1"),
            exchange = @Exchange(name = "moresuo.direct", type = ExchangeTypes.DIRECT),
            key = {"red", "blue"}
    ))
    public void testTopicQueue1(String msg) {
        System.out.println("消费者1接收到Topic的消息：[" + msg + "]");
    }

    @RabbitListener(queues = "topic.queue2")
    public void testTopicQueue2(String msg) {
        System.out.println("消费者2接收到Topic的消息：[" + msg + "]");
    }

    @RabbitListener(queues = "object.queue")
    public void testListenMap(Map<String, Object> map){
        System.out.println("消费者接收到object.queue的消息：[" + map + "]");
    }
}
